package cn.niit.letuseatdemo.service.impl;

import cn.niit.letuseatdemo.entity.domain.OrderGoods;
import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.mapper.OrderGoodsMapper;
import cn.niit.letuseatdemo.service.OrderGoodsService;
import cn.niit.letuseatdemo.util.LoginStoreUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 订单service接口实现类
 * @Author: su_fuming
 * @CreateTime： 2022/11/8 21:52
 */
@Service
public class OrderGoodsServiceImpl implements OrderGoodsService {
    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public OrderGoods selectOrderGoodsByOrderId(Long orderId)
    {
        return orderGoodsMapper.selectOrderGoodsByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param orderGoods 订单
     * @return 订单
     */
    @Override
    public List<OrderGoods> selectOrderGoodsList(OrderGoods orderGoods)
    {
        /**
         *  开启分页查询
         *  PageHelper.startPage(goods.getPage(),goods.getRows())
         *  1.将改代码放在要执行分页查询语句之前
         *   2.两个参数，分别是当前第几页，每页记录数
         **/
        if(orderGoods.getPage() == null){
            orderGoods.setPage(1);
        }
        PageHelper.startPage(orderGoods.getPage(),orderGoods.getRows());
        List<OrderGoods> orderGoodsList = orderGoodsMapper.selectOrderGoodsList(orderGoods);
        return orderGoodsList;
    }

    /**
     * 新增订单
     *
     * @param orderGoods 订单
     * @return 结果
     */
    @Override
    public int insertOrderGoods(OrderGoods orderGoods)
    {
        /** 为delFlag 赋予默认值*/
        orderGoods.setDelFlag(0);
        /** 获取用户信息，为department 赋值，创建人信息（id,姓名)，创建时间 */
        Store loginStore = LoginStoreUtil.getLoginStore();
        orderGoods.setCreateUserId(loginStore.getStoreId());
        orderGoods.setCreateUserName(loginStore.getStoreName());
        orderGoods.setGmtCreate(new Date());
        return orderGoodsMapper.insertOrderGoods(orderGoods);
    }

    /**
     * 修改订单
     *
     * @param orderGoods 订单
     * @return 结果
     */
    @Override
    public int updateOrderGoods(OrderGoods orderGoods)
    {
        Store loginStore = LoginStoreUtil.getLoginStore();
        orderGoods.setUpdateUserId(loginStore.getStoreId());
        orderGoods.setUpdateUserName(loginStore.getStoreName());
        orderGoods.setGmtUpdate(new Date());
        return orderGoodsMapper.updateOrderGoods(orderGoods);
    }
    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderGoodsByOrderIds(String orderIds)
    {
        return 0;
    }
    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrderGoodsByOrderId(Long orderId)
    {
        return orderGoodsMapper.deleteOrderGoodsByOrderId(orderId);
    }
    /**
     * 修订单状态
     *
     * @param
     * @return 结果
     */
    @Override
    public int updateOrderStatus(OrderGoods orderGoods) {
        return orderGoodsMapper.updateOrderStatus(orderGoods);
    }
}

